public class LeetCode1008 {
    public TreeNode bstFromPreorder(int[] preorder) {
        if (preorder.length == 0) {
            return null;
        }
        //创建根节点
        TreeNode root = new TreeNode(preorder[0]);
        for (int i = 1; i < preorder.length; i++) {
            int val = preorder[i];
            insert(root,val);
        }
        return root;
    }

    //直接插入 可以参考之前写的 力扣 701
    private TreeNode insert(TreeNode node, int val) {
        if (node == null) {
            return new TreeNode(val);
        }
        if (val < node.val) {
            node.left = insert(node.left, val);
        }
        if(node.val < val){
            node.right = insert(node.right, val);
        }
        return node;
    }
}
